<template>
  <el-popover width="50%"
              @show="search"
              trigger="hover">
    <el-table :data="tableData"
              stripe
              border
              v-loading="loading">
      <el-table-column prop="county"
                       label="所属县"
                       align='center'
                       width="100">
      </el-table-column>
      <el-table-column prop="stcd"
                       label="站点编码"
                       align='center'
                       width="100">
      </el-table-column>
      <el-table-column prop="stnm"
                       align='center'
                       label="站点名称"
                       min-width="120">
      </el-table-column>
      <el-table-column prop="sttp"
                       align='center'
                       label="测站类型"
                       width="100">
      </el-table-column>
      <el-table-column prop="tm"
                       align='center'
                       label="最后来报"
                       width="120">
        <template slot-scope="props">
          <span v-if="props.row.tm.indexOf('0001-01-01') != -1"
                :class="{'table-background-color-red' : props.row.isEnabled === 'TRUE'}">历史无来报!</span>
          <span v-else-if="renderTm(props.row.tm)"
                :class="{'table-background-color-red' : props.row.isEnabled === 'TRUE'}">{{ props.row.tm }}</span>
          <span v-else>{{ props.row.tm }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="submittedName"
                       align='center'
                       width="130"
                       label="上报单位">
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination layout="total, sizes, prev, pager, next, jumper"
                   :total="pagging.total"
                   :page-sizes="pagging.sizes"
                   :current-page="pagging.currentPage"
                   @size-change="sizeChange"
                   @current-change="pageChange">
    </el-pagination>
    <slot slot="reference"></slot>
  </el-popover>
</template>

<script>
import { getLastTm } from '@/api/site.js'
import Moment from 'moment'
export default {
  name: 'siteHover',
  props: {
    submittedUnit: {
      type: String
    },
    status: {
      type: String
    },
    isEnabled: {
      type: Boolean
    },
    cbmu: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      tableData: [],
      // 分页属性
      pagging: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        sizes: [10, 30, 50, 100]
      }
    }
  },
  methods: {
    search() {
      this.pagging.currentPage = 1
      this.getTableData()
    },
    // 每页显示条数变更新
    sizeChange(size) {
      this.pagging.pageSize = size
      this.getTableData()
    },
    // 跳转到页面
    pageChange(curPage) {
      this.pagging.currentPage = curPage
      this.getTableData()
    },
    renderTm(tm) {
      if (tm.indexOf('0001-01-01') !== -1) return false
      var hour = Moment().diff(Moment(tm), 'hour')
      if (hour >= 12) return true
      return false
    },
    // 请求数据
    getTableData() {
      this.loading = true
      const params = {
        submittedUnit: this.submittedUnit,
        status: this.status,
        cbmu: this.cbmu,
        isEnabled: this.isEnabled,
        pageIndex: this.pagging.currentPage,
        pageSize: this.pagging.pageSize
      }
      getLastTm(params).then(res => {
        if (res.data.success) {
          const result = res.data.data
          this.tableData = result.data
          this.pagging.total = result.totalCount
          this.pagging.currentPage = result.pageIndex
          this.pagging.pageSize = result.pageSize
          this.loading = false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
